```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Elasticsearch评分机制深度解析 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);
        }
        .formula-card {
            background-color: #f8f9fa;
            border-left: 4px solid #667eea;
        }
        .drop-cap {
            font-size: 3.5em;
            line-height: 0.8;
            float: left;
            margin-right: 0.1em;
            font-weight: bold;
            color: #667eea;
        }
        a:hover {
            color: #764ba2;
            text-decoration: underline;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 md:py-32">
        <div class="container mx-auto px-6 md:px-12 text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-4 font-serif">Elasticsearch评分机制解析</h1>
            <p class="text-xl md:text-2xl mb-8 max-w-3xl mx-auto">深入理解TF-IDF与BM25算法的核心原理</p>
            <div class="flex justify-center space-x-4">
                <a href="#tfidf" class="px-6 py-3 bg-white text-indigo-700 font-semibold rounded-full hover:bg-gray-100 transition duration-300">TF-IDF算法</a>
                <a href="#bm25" class="px-6 py-3 border-2 border-white text-white font-semibold rounded-full hover:bg-white hover:text-indigo-700 transition duration-300">BM25算法</a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 md:px-12 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <i class="fas fa-info-circle text-indigo-600 text-2xl mr-3"></i>
                <h2 class="text-2xl font-bold text-gray-800">评分机制概述</h2>
            </div>
            <div class="bg-white rounded-xl p-6 card">
                <p class="text-gray-700 mb-4">
                    <span class="drop-cap">E</span>lasticsearch（ES）的评分（score）计算是基于 <strong class="text-indigo-600">TF-IDF</strong> 和 <strong class="text-indigo-600">BM25</strong> 算法的，具体取决于使用的相似度算法。评分是用来衡量文档与查询的相关性，通常用于检索结果的排序。
                </p>
                <div class="my-8 rounded-lg overflow-hidden">
                    <img src="https://cdn.nlark.com/yuque/0/2021/png/21449790/1636548395809-4f96338c-d687-4cd2-be0e-9c45cebfdfc2.png" alt="Elasticsearch评分机制" class="w-full h-auto rounded-lg shadow-md">
                </div>
                <p class="text-gray-700">
                    在信息检索领域，评分算法决定了搜索结果的排序质量。Elasticsearch从早期版本的TF-IDF算法逐渐过渡到更先进的BM25算法，这一转变带来了更精确的搜索结果和更好的用户体验。
                </p>
            </div>
        </section>

        <!-- TF-IDF Section -->
        <section id="tfidf" class="mb-16">
            <div class="flex items-center mb-6">
                <i class="fas fa-calculator text-indigo-600 text-2xl mr-3"></i>
                <h2 class="text-2xl font-bold text-gray-800">1. TF-IDF算法</h2>
            </div>
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl p-6 card">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-700">TF-IDF（Term Frequency-Inverse Document Frequency）</h3>
                    <p class="text-gray-700 mb-6">一种常见的文本检索和信息检索算法，用于评估一个词在文档中的重要性。</p>
                    
                    <div class="formula-card rounded-lg p-4 mb-6">
                        <h4 class="font-medium text-gray-800 mb-2 flex items-center">
                            <i class="fas fa-square-root-alt mr-2 text-indigo-500"></i> TF（词频）公式
                        </h4>
                        <div class="bg-white p-3 rounded">
                            <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1723789446150-ba582167-2c52-4d9f-8687-b20b21fcc840.png" alt="TF公式" class="w-full h-auto">
                        </div>
                    </div>
                    
                    <div class="formula-card rounded-lg p-4 mb-6">
                        <h4 class="font-medium text-gray-800 mb-2 flex items-center">
                            <i class="fas fa-function mr-2 text-indigo-500"></i> IDF（逆文档频率）公式
                        </h4>
                        <div class="bg-white p-3 rounded">
                            <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1723789461292-200102a3-6f22-438b-9649-9ff428102a3e.png" alt="IDF公式" class="w-full h-auto">
                        </div>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl p-6 card">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-700">TF-IDF 评分</h3>
                    <p class="text-gray-700 mb-6">TF 和 IDF 的乘积构成了TF-IDF评分的基本计算公式。</p>
                    
                    <div class="formula-card rounded-lg p-4 mb-6">
                        <h4 class="font-medium text-gray-800 mb-2 flex items-center">
                            <i class="fas fa-equals mr-2 text-indigo-500"></i> TF-IDF 评分公式
                        </h4>
                        <div class="bg-white p-3 rounded">
                            <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1723789469657-ccfd9969-dd3d-44e3-9174-0cf54b3a1ab9.png" alt="TF-IDF公式" class="w-full h-auto">
                        </div>
                    </div>
                    
                    <div class="bg-blue-50 border-l-4 border-blue-400 p-4 rounded-r-lg">
                        <h4 class="font-medium text-blue-800 mb-2 flex items-center">
                            <i class="fas fa-lightbulb mr-2 text-blue-500"></i> 算法特点
                        </h4>
                        <ul class="text-gray-700 space-y-2">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-400 mr-2 mt-1"></i>
                                <span>简单直观，易于理解和实现</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-400 mr-2 mt-1"></i>
                                <span>对短文档效果较好</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-exclamation-circle text-yellow-500 mr-2 mt-1"></i>
                                <span>未考虑文档长度因素，可能对长文档不公平</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- BM25 Section -->
        <section id="bm25" class="mb-16">
            <div class="flex items-center mb-6">
                <i class="fas fa-chart-line text-indigo-600 text-2xl mr-3"></i>
                <h2 class="text-2xl font-bold text-gray-800">2. BM25算法</h2>
            </div>
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl p-6 card">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-700">BM25（Best Match 25）</h3>
                    <p class="text-gray-700 mb-6">一种改进的评分函数，常用于 Elasticsearch 7.x 及以后的版本。BM25 算法是 <strong>Okapi BM模型</strong> 的一个变体，其评分函数考虑了词频和文档长度的规范化，适用于多种检索场景。</p>
                    
                    <div class="formula-card rounded-lg p-4 mb-6">
                        <h4 class="font-medium text-gray-800 mb-2 flex items-center">
                            <i class="fas fa-square-root-alt mr-2 text-indigo-500"></i> BM25 评分函数
                        </h4>
                        <div class="bg-white p-3 rounded">
                            <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1723789482680-3d0a9fd1-a71a-40d6-b134-739d991c3fed.png" alt="BM25公式" class="w-full h-auto">
                        </div>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl p-6 card">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-700">BM25 参数说明</h3>
                    <p class="text-gray-700 mb-6">BM25算法引入了多个参数来优化评分效果：</p>
                    
                    <div class="bg-white p-4 rounded-lg">
                        <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1723789514962-a7e56607-4b56-42b5-8556-e462a7a251ff.png" alt="BM25参数" class="w-full h-auto mb-6">
                    </div>
                    
                    <div class="bg-blue-50 border-l-4 border-blue-400 p-4 rounded-r-lg">
                        <h4 class="font-medium text-blue-800 mb-2 flex items-center">
                            <i class="fas fa-lightbulb mr-2 text-blue-500"></i> 算法优势
                        </h4>
                        <ul class="text-gray-700 space-y-2">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-400 mr-2 mt-1"></i>
                                <span>考虑文档长度，避免长文档获得不合理的优势</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-400 mr-2 mt-1"></i>
                                <span>参数可调，适应不同的搜索场景</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-400 mr-2 mt-1"></i>
                                <span>在大多数情况下优于传统的TF-IDF</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- ES评分流程 -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <i class="fas fa-sitemap text-indigo-600 text-2xl mr-3"></i>
                <h2 class="text-2xl font-bold text-gray-800">3. ES评分计算过程</h2>
            </div>
            <div class="bg-white rounded-xl p-6 card">
                <div class="mb-6">
                    <p class="text-gray-700 mb-6">在Elasticsearch中，评分的计算过程是一个多阶段的流程，每个阶段都对最终的相关性评分有所贡献。</p>
                    
                    <div class="relative">
                        <!-- Mermaid流程图 -->
                        <div class="mermaid">
                            graph TD
                                A[查询解析] --> B[文档检索]
                                B --> C[评分计算]
                                C --> D[排序结果]
                                C -->|TF-IDF| E[应用TF-IDF公式]
                                C -->|BM25| F[应用BM25公式]
                                D --> G[返回最相关文档]
                        </div>
                    </div>
                </div>
                
                <div class="space-y-4">
                    <div class="flex items-start">
                        <div class="flex-shrink-0 bg-indigo-100 rounded-full p-2 mr-4">
                            <i class="fas fa-search text-indigo-600"></i>
                        </div>
                        <div>
                            <h4 class="font-medium text-gray-800 mb-1">1. 查询解析</h4>
                            <p class="text-gray-600">将用户的查询请求解析为Elasticsearch可以理解的查询条件。</p>
                        </div>
                    </div>
                    
                    <div class="flex items-start">
                        <div class="flex-shrink-0 bg-indigo-100 rounded-full p-2 mr-4">
                            <i class="fas fa-database text-indigo-600"></i>
                        </div>
                        <div>
                            <h4 class="font-medium text-gray-800 mb-1">2. 文档检索</h4>
                            <p class="text-gray-600">在索引中找到与查询条件匹配的文档集合。</p>
                        </div>
                    </div>
                    
                    <div class="flex items-start">
                        <div class="flex-shrink-0 bg-indigo-100 rounded-full p-2 mr-4">
                            <i class="fas fa-calculator text-indigo-600"></i>
                        </div>
                        <div>
                            <h4 class="font-medium text-gray-800 mb-1">3. 评分计算</h4>
                            <p class="text-gray-600">根据查询条件和文档内容，计算每个文档的评分。Elasticsearch会根据配置选择使用TF-IDF或BM25算法。</p>
                        </div>
                    </div>
                    
                    <div class="flex items-start">
                        <div class="flex-shrink-0 bg-indigo-100 rounded-full p-2 mr-4">
                            <i class="fas fa-sort-amount-down text-indigo-600"></i>
                        </div>
                        <div>
                            <h4 class="font-medium text-gray-800 mb-1">4. 排序</h4>
                            <p class="text-gray-600">根据计算出的评分对文档进行降序排序，并返回最相关的文档作为查询结果。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Custom Score & Example -->
        <section class="mb-16">
            <div class="grid md:grid-cols-2 gap-8">
                <!-- 自定义评分 -->
                <div>
                    <div class="flex items-center mb-6">
                        <i class="fas fa-sliders-h text-indigo-600 text-2xl mr-3"></i>
                        <h2 class="text-2xl font-bold text-gray-800">4. 自定义评分</h2>
                    </div>
                    <div class="bg-white rounded-xl p-6 card h-full">
                        <p class="text-gray-700 mb-4">
                            Elasticsearch 允许用户通过自定义评分脚本（使用 <code class="bg-gray-100 px-2 py-1 rounded">script_score</code> 查询）或自定义评分函数（如 <code class="bg-gray-100 px-2 py-1 rounded">function_score</code> 查询）来影响评分计算。
                        </p>
                        <div class="bg-gray-50 p-4 rounded-lg mb-4">
                            <h4 class="font-medium text-gray-800 mb-2 flex items-center">
                                <i class="fas fa-code mr-2 text-indigo-500"></i> function_score示例
                            </h4>
                            <pre class="bg-gray-800 text-gray-100 p-3 rounded text-sm overflow-x-auto">
{
  "query": {
    "function_score": {
      "query": { "match": { "title": "机器学习" } },
      "functions": [
        {
          "filter": { "term": { "category": "高级教程" } },
          "weight": 2
        }
      ],
      "score_mode": "sum"
    }
  }
}</pre>
                        </div>
                        <p class="text-gray-700">
                            这些自定义方法允许更灵活的评分策略，可以结合业务需求调整文档的权重，例如提升付费内容、热门内容或特定类别内容的排名。
                        </p>
                    </div>
                </div>
                
                <!-- 评分示例 -->
                <div>
                    <div class="flex items-center mb-6">
                        <i class="fas fa-list-ol text-indigo-600 text-2xl mr-3"></i>
                        <h2 class="text-2xl font-bold text-gray-800">5. 评分示例</h2>
                    </div>
                    <div class="bg-white rounded-xl p-6 card h-full">
                        <p class="text-gray-700 mb-4">假设有两个文档和一个查询词"机器学习"：</p>
                        
                        <div class="grid gap-4 mb-6">
                            <div class="border border-gray-200 rounded-lg p-4">
                                <h4 class="font-medium text-gray-800 mb-2">文档 1</h4>
                                <ul class="text-gray-600 space-y-1">
                                    <li>包含"机器学习"词 3 次</li>
                                    <li>文档总词数 100</li>
                                </ul>
                            </div>
                            
                            <div class="border border-gray-200 rounded-lg p-4">
                                <h4 class="font-medium text-gray-800 mb-2">文档 2</h4>
                                <ul class="text-gray-600 space-y-1">
                                    <li>包含"机器学习"词 1 次</li>
                                    <li>文档总词数 200</li>
                                </ul>
                            </div>
                            
                            <div class="border border-gray-200 rounded-lg p-4 bg-indigo-50">
                                <h4 class="font-medium text-gray-800 mb-2">查询</h4>
                                <p class="text-gray-600">查询词"机器学习"</p>
                            </div>
                        </div>
                        
                        <div class="space-y-4">
                            <div class="bg-blue-50 border-l-4 border-blue-400 p-4 rounded-r-lg">
                                <h4 class="font-medium text-blue-800 mb-2">TF-IDF 评分</h4>
                                <p class="text-gray-700 text-sm">
                                    文档1可能会获得更高分数，因为词频更高，且TF-IDF不考虑文档长度。
                                </p>
                            </div>
                            
                            <div class="bg-green-50 border-l-4 border-green-400 p-4 rounded-r-lg">
                                <h4 class="font-medium text-green-800 mb-2">BM25 评分</h4>
                                <p class="text-gray-700 text-sm">
                                    文档2可能获得更合理的分数，因为BM25会考虑文档长度，防止长文档因简单重复而获得过高评分。
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Key Takeaways -->
        <section class="mb-16">
            <div class="bg-indigo-700 rounded-xl p-8 text-white">
                <div class="flex items-center mb-6">
                    <i class="fas fa-star text-yellow-300 text-2xl mr-3"></i>
                    <h2 class="text-2xl font-bold">核心要点总结</h2>
                </div>
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="flex items-start">
                        <div class="flex-shrink-0 mt-1">
                            <i class="fas fa-check-circle text-green-300"></i>
                        </div>
                        <div class="ml-3">
                            <h4 class="font-semibold mb-1">TF-IDF适用于简单场景</h4>
                            <p class="text-indigo-100 opacity-90">传统算法，易于理解，但对长文档可能不公平</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <div class="flex-shrink-0 mt-1">
                            <i class="fas fa-check-circle text-green-300"></i>
                        </div>
                        <div class="ml-3">
                            <h4 class="font-semibold mb-1">BM25是更优选择</h4>
                            <p class="text-indigo-100 opacity-90">现代算法，考虑文档长度，参数可调，效果更好</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <div class="flex-shrink-0 mt-1">
                            <i class="fas fa-check-circle text-green-300"></i>
                        </div>
                        <div class="ml-3">
                            <h4 class="font-semibold mb-1">评分流程清晰</h4>
                            <p class="text-indigo-100 opacity-90">查询解析 → 文档检索 → 评分计算 → 结果排序</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <div class="flex-shrink-0 mt-1">
                            <i class="fas fa-check-circle text-green-300"></i>
                        </div>
                        <div class="ml-3">
                            <h4 class="font-semibold mb-1">灵活自定义</h4>
                            <p class="text-indigo-100 opacity-90">可通过function_score等定制评分策略</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto px-6 md:px-12">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-semibold text-white mb-1">技术小馆</h3>
                    <p class="text-gray-400">深入探索技术原理，分享开发实战经验</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition duration-300 flex items-center">
                        <i class="fas fa-external-link-alt mr-2"></i>
                        <span>访问语雀专栏</span>
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-6 pt-6 text-center text-sm text-gray-500">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        // 初始化Mermaid图表
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```